import os
import csv
import numpy as np
import matplotlib.pyplot as plt


def fn1():
    points = []
    for i in range(300):
        x = round(np.random.uniform(0, 20), 2)
        noise = np.random.normal(0, 1)
        y = round(0.825 * x + noise, 2)
        points.append([x, y])
        plt.scatter(x, y, c='r')
    plt.show()

    print(points)
    with open('线性回归.csv', mode='a', newline='', encoding='utf-8-sig') as f:
        for p in points:
            csv_writer = csv.writer(f, delimiter=',')
            csv_writer.writerow(p)


def fn2():

    item = {'title': '穆斯林的葬礼', 'asin': '', 'url': '', 'brand': ''}
    fieldnames = ['title', 'asin', 'url', 'brand']
    with open('数据.csv', mode='a', newline='', encoding='utf-8-sig') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        # 判断表格内容是否为空，如果为空就添加表头
        if not os.path.getsize('数据.csv'):
            writer.writeheader()  # 写入表头
        writer.writerows([item])

    urls = ['https://www.xx.com', 'https://www.yy.com']
    with open('test.csv', mode='a', newline='', encoding='utf-8-sig') as f:
        csv_writer = csv.writer(f, delimiter=',')
        csv_writer.writerow([urls])





if __name__ == '__main__':
    fn1()
    # fn2()
